package com.pig4cloud.pig.module.service.estate.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pig4cloud.pig.common.core.exception.TransactionalException;
import com.pig4cloud.pig.module.api.entity.estate.EstateRoom;
import com.pig4cloud.pig.module.api.entity.property.PropertyFeeOrderTemplate;
import com.pig4cloud.pig.module.mapper.estate.EstateRoomMapper;
import com.pig4cloud.pig.module.mapper.property.PropertyFeeOrderTemplateMapper;
import com.pig4cloud.pig.module.service.estate.IEstateRoomService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 小区内的房屋列信息Service业务层处理
 *
 * @author ruoyi
 * @date 2022-06-28
 */
@Service
public class EstateRoomServiceImpl extends ServiceImpl<EstateRoomMapper, EstateRoom> implements IEstateRoomService {

	@Resource
	private EstateRoomMapper estateRoomMapper;
	@Resource
	private PropertyFeeOrderTemplateMapper feeOrderTemplateMapper;

	@Override
	@Transactional
	public int updateRoom(EstateRoom estateRoom) {
		int ret = 0;
		try{
			ret = estateRoomMapper.updateById(estateRoom);
			if(ret <= 0){
				throw new TransactionalException("estateRoomMapper insert error");
			}

			//同步一下 数据模板里的  业主名称  手机号
			QueryWrapper<PropertyFeeOrderTemplate> qw = new QueryWrapper<>();
			qw.eq("room_id",estateRoom.getId());
			List<PropertyFeeOrderTemplate> templates = feeOrderTemplateMapper.selectList(qw);
			if(CollUtil.isNotEmpty(templates)){
				for (PropertyFeeOrderTemplate template : templates){
					template.setRealName(estateRoom.getRealName());
					template.setPhoneNum(estateRoom.getPhoneNum());
					ret = feeOrderTemplateMapper.updateById(template);
					if(ret <= 0){
						throw new TransactionalException("feeOrderTemplateMapper insert error");
					}
				}
			}
			ret = 1;
		}catch (Exception e){
			e.printStackTrace();
			throw new TransactionalException("updateRoom insert error");
		}
		return ret;
	}
}
